package com.xm.controller;

import com.xm.entity.User;
import com.xm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(String username, String password) {
        System.out.println("welcome to login");
        boolean success = userService.login(username, password);
        return success ? "登录成功" : "用户名或密码错误";
    }

    @PostMapping("/register")
    public String register(String username, String password) {
        System.out.println("welcome to register");
        boolean success = userService.register(username, password);
        return success ? "注册成功" : "用户名已存在";
    }

    @PutMapping("/update")
    public String updata(User user) {
        System.out.println("更新");
        int result = userService.updateUser(user);
        return result != 0 ? "更新成功" : "更新失败";
    }

    @DeleteMapping("/delete")
    public String delete(Integer id) {
        System.out.println("---删除---");
        Integer result = userService.deleteUser(id);
        return result != 0 ? "删除成功" : "删除失败";
    }

    @GetMapping("/get")
    public String getUser(Integer id) {
        System.out.println("---查询---");
        User user = userService.getUserById(id);
        System.out.println(user);
        return user != null ?"成功"+user:"失败"+user;
    }

    @GetMapping("/getAllUsers")
    public String getAllUsers() {
        System.out.println("---查询所有用户---");
        List<User> allUsers = userService.getAllUsers();
        System.out.println(allUsers);
        return allUsers != null ?"成功 "+allUsers:"失败";
    }

    @PostMapping("/test")
    public String test() {
        System.out.println("test");
        return "test";
    }
}
